package com.ruoyi.industryfrontend.service.impl;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.uuid.IdUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.industryfrontend.domain.ProjectApartment;
import com.ruoyi.industryfrontend.domain.ProjectPark;
import com.ruoyi.industryfrontend.domain.SpaceIndustry;
import com.ruoyi.industryfrontend.mapper.*;
import com.ruoyi.industryfrontend.service.IProjectApartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公寓Service业务层处理
 *
 * @author MagicCube
 * @date 2023-05-08
 */
@Service
public class ProjectApartmentServiceImpl implements IProjectApartmentService
{
    @Autowired
    private ProjectApartmentMapper projectApartmentMapper;

    @Autowired
    private DistrictMapper districtMapper;

    @Autowired
    private ProjectParkMapper projectParkMapper;

    @Autowired
    private SpaceIndustryMapper spaceIndustryMapper;

    @Autowired
    private SpaceCollectMapper spaceCollectMapper;
    /**
     * 查询公寓
     *
     * @param id 公寓主键
     * @return 公寓
     */
    @Override
    public ProjectApartment selectProjectApartmentById(Long id)
    {
        return projectApartmentMapper.selectProjectApartmentById(id);
    }

    /**
     * 查询公寓
     * @param spaceId 编号
     * @return 结果
     */
    @Override
    public Map<String, Object> selectProjectApartmentByNumber(String spaceId) {
        ProjectApartment projectApartment = projectApartmentMapper.selectProjectApartmentByNumber(spaceId);
        List<String> list = spaceCollectMapper.selectSpaceCollectByUserId(SecurityUtils.getUserId(),spaceId);
        if (list!=null&&list.size()!=0){
            projectApartment.setCollectState(1L);
        }else {
            projectApartment.setCollectState(0L);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("space",projectApartment);
        if (projectApartment.getParkId()!=null) {
            ProjectPark projectPark = projectParkMapper.selectProjectParkById(projectApartment.getParkId());
            map.put("park",projectPark);
        }
        return map;
    }

    /**
     * 查询公寓列表
     *
     * @param projectApartment 公寓
     * @return 公寓
     */
    @Override
    public List<ProjectApartment> selectProjectApartmentList(ProjectApartment projectApartment)
    {
        //租赁价格
        if (projectApartment.getApartmentLeasePrice()!=null){
            String[] split = projectApartment.getApartmentLeasePrice().split("~");
            projectApartment.setApartmentLeasePriceLow(Long.parseLong(split[0]));
            if (!split[1].equals("N")) {
                projectApartment.setApartmentLeasePriceHigh(Long.parseLong(split[1]));
            }
        }
//        //所属城市
//        List<Long> districtIds = new ArrayList<Long>();
//        if (projectApartment.getCityId()!=null){
//            districtIds.add(projectApartment.getCityId());
//            projectApartment.setCityIds(districtIds);
//        }else if (projectApartment.getCityId()==null && projectApartment.getProvinceId() != null){
//            //查询省份下的城市
//            districtIds.addAll(districtMapper.selectDistrictIdList(projectApartment.getProvinceId()));
//            districtIds.add(projectApartment.getProvinceId());
//            projectApartment.setCityIds(districtIds);
//        }
        projectApartment.setSupplyUserId(SecurityUtils.getUserId());
        return projectApartmentMapper.selectProjectApartmentListz(projectApartment);
    }

    /**
     * 新增公寓
     *
     * @param projectApartment 公寓
     * @return 结果
     */
    @Override
    public int insertProjectApartment(ProjectApartment projectApartment)
    {
        projectApartment.setSupplyUserId(SecurityUtils.getUserId());
        projectApartment.setApprovalState(0L);
        projectApartment.setCreateTime(DateUtils.getNowDate());
        projectApartment.setSerialNumber(IdUtils.fastSimpleUUID());
        SpaceIndustry spaceIndustry = new SpaceIndustry();
        extracted(projectApartment, spaceIndustry);
        spaceIndustry.setCreateBy(SecurityUtils.getUsername());
        spaceIndustry.setCreateTime(projectApartment.getCreateTime());
        spaceIndustry.setSpaceId(projectApartment.getSerialNumber());
        spaceIndustry.setSupplyUserId(SecurityUtils.getUserId());
        spaceIndustryMapper.insertSpaceIndustry(spaceIndustry);
        return projectApartmentMapper.insertProjectApartment(projectApartment);
    }

    private static void extracted(ProjectApartment projectApartment, SpaceIndustry spaceIndustry) {
        spaceIndustry.setBuildType(6);
        spaceIndustry.setReleaseType(0L);//供给
        spaceIndustry.setApprovalState(projectApartment.getApprovalState());
        spaceIndustry.setProvinceId(projectApartment.getProvinceId());//省份
        spaceIndustry.setCityId(projectApartment.getCityId());//城市
//        spaceIndustry.setAreaId();//区域
        spaceIndustry.setSupplyType(projectApartment.getSupplyType());//服务类型
        spaceIndustry.setParkId(String.valueOf(projectApartment.getParkId()));
        spaceIndustry.setHeating(projectApartment.getApartmentHeating());//供暖
        spaceIndustry.setRefrig(projectApartment.getApartmentRefrig());//制冷
        spaceIndustry.setPower(projectApartment.getApartmentPower());//电力
        spaceIndustry.setGas(projectApartment.getApartmentGas());//燃气
        spaceIndustry.setLeasePrice(projectApartment.getApartmentLeasePrice());//出售价格
        spaceIndustry.setSellPrice(projectApartment.getApartmentSellPrice());//租赁价格
        spaceIndustry.setCoverResource(projectApartment.getCoverResource());//封面资源
        spaceIndustry.setSpaceName(projectApartment.getApartmentName());//园区名称
        spaceIndustry.setSpaceAllSize(projectApartment.getApartmentAllSize());//空间总面积
        spaceIndustry.setTop(projectApartment.getTop());//置顶
    }

    /**
     * 修改公寓
     *
     * @param projectApartment 公寓
     * @return 结果
     */
    @Override
    public int updateProjectApartment(ProjectApartment projectApartment)
    {
        projectApartment.setApprovalState(0L);
        projectApartment.setUpdateTime(DateUtils.getNowDate());
        SpaceIndustry spaceIndustry = new SpaceIndustry();
        extracted(projectApartment,spaceIndustry);
        spaceIndustry.setSpaceId(String.valueOf(projectApartment.getId()));
        spaceIndustry.setUpdateBy(SecurityUtils.getUsername());
        spaceIndustry.setUpdateTime(projectApartment.getUpdateTime());
        spaceIndustryMapper.updateSpaceIndustryByApartmentId(spaceIndustry);
        return projectApartmentMapper.updateProjectApartment(projectApartment);
    }

    /**
     * 批量删除公寓
     *
     * @param ids 需要删除的公寓主键
     * @return 结果
     */
    @Override
    public int deleteProjectApartmentByIds(Long[] ids)
    {
        return projectApartmentMapper.deleteProjectApartmentByIds(ids);
    }

    /**
     * 删除公寓信息
     *
     * @param id 公寓主键
     * @return 结果
     */
    @Override
    public int deleteProjectApartmentById(Long id)
    {
        spaceIndustryMapper.deleteSpaceIndustryByProjectParkId(id);
        return projectApartmentMapper.deleteProjectApartmentById(id);
    }

    @Override
    public int deleteProjectApartmentBySerialNumber(String spaceId) {
        spaceIndustryMapper.deleteSpaceIndustryBySerialNumber(spaceId);
        return projectApartmentMapper.deleteProjectApartmentBySerialNumber(spaceId);
    }
}
